package com.team.panicgame.models.vudduu;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.team.panicgame.GameConstants;
import com.team.panicgame.models.InputStatus;
import com.team.panicgame.models.vudduu.VudduuPlayer.PLAYER_STATE;

public class VudduuWorld {

	private InputStatus inputStatus;
	private VudduuPlayer player;
	
	public static float MAP_CELL_SIZE = 3;
	public static float MAP_WIDTH = GameConstants.GAME_WIDTH / MAP_CELL_SIZE + 1;
	public static float MAP_HEIGHT = GameConstants.GAME_HEIGHT / MAP_CELL_SIZE + 1;
	public int[][] map;
	public String[] read;
	public int xini, yini;
	
	public VudduuWorld(InputStatus inputStatus) throws IOException {
		this.inputStatus = inputStatus;		
		openMapFile();
	}
	
	public void openMapFile() throws IOException {
		InputStream in = Gdx.files.internal("data/vudduu.map").read();
		InputStreamReader is = new InputStreamReader(in);
		BufferedReader br = new BufferedReader(is);
		int it = 0;
		read = new String[32];
		read[it] = br.readLine();
		while(read[it] != null) {
			it++;
		    read[it] = br.readLine();
		}
		in.close();
		map = new int[read[0].length()][it];
		for(int i=0; i < read.length-1 ;i++) {
			for(int j=0; j < read[i].length() ;j++) {
				if(read[i].charAt(j) == '1') {
					map[j][read.length-2-i] = 1;
				}
				else if(read[i].charAt(j) == 'X') {
					map[j][read.length-2-i] = 100;
				}
				else {
					map[j][read.length-2-i] = 0;
				}
			}
		}
	}
	
	public void update(float delta) {
		if(inputStatus.isLeft) {
			player.increaseX(this, -1.0f);
		}
		if(inputStatus.isRight) {
			player.increaseX(this, 1.0f);
		}
		if(inputStatus.isDown){
			System.out.println("DOWN");
		}
		if(inputStatus.isUp){
			player.jump();
		}
	}
	
	public Vector2 coordinatePlayer() {
		for(int x=0; x<map.length ;x++) {
			for(int y=map[x].length-1; y>=0 ;y--) {
				if(map[x][y] == 100) {
					return new Vector2(x, y);
				}
			}
		}
		return new Vector2(-1, -1);
	}
	
	public void setPlayer(VudduuPlayer player) {
		this.player = player;
	}
	
	public int[][] getActualMap(Vector2 pos) {
		int[][] ret = new int[(int)MAP_WIDTH][(int)MAP_HEIGHT];
		
		int xc = (int) pos.x;
		xini = xc - ((int)MAP_WIDTH/2);
		int yc = (int) pos.y;
		yini = yc - ((int)MAP_HEIGHT/2);
		System.out.println("---");
		System.out.println((int)MAP_WIDTH + " " + (int)(MAP_WIDTH/2));
		System.out.println((int)MAP_HEIGHT + " " + (int)(MAP_HEIGHT/2));
		System.out.println(xc + " " + yc + " , " + xini + " " + yini);
		
		for(int i=xini; i < xini+(int)MAP_WIDTH ;i++) {
			for(int j=yini; j < yini+(int)MAP_HEIGHT ;j++) {
				ret[i-xini][j-yini] = map[i][j];
			}
		}
		return ret;
	}

}
